package zsc.nine.onlinemall.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import zsc.nine.onlinemall.entity.User;
import zsc.nine.onlinemall.service.UserService;

import java.util.Date;
import java.util.List;

/**
 * author: 莫远烙
 */
@RequestMapping("/User")
@RestController
public class UserController {
    @Autowired
    UserService userService;

    /**
     *
     * @param UserEmail
     * @param UserName
     * @param Password
     * @param UserAddress
     * @return
     */
    //注册用户
    @PostMapping("/Register")
    public String UserRegister(@RequestParam(value = "UserEmail", required = false) String UserEmail,
                               @RequestParam(value = "UserName", required = false) String UserName,
                               @RequestParam(value = "Password", required = false) String Password,
                               @RequestParam(value = "UserAddress", required = false) String UserAddress) {

        /*初始化返回结果，并将结果RegisterResult字段置为Fail*/
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("UserRegisterResult", "Fail");

        /*后端校验数据合法性*/
        if (UserEmail == null || UserName == null || Password == null) {
            jsonObject.put("UserRegisterResult", "Parameter-Null");

        } else if (UserEmail.trim().isEmpty() || UserName.trim().isEmpty() || Password.trim().isEmpty()) {
            jsonObject.put("UserRegisterResult", "Empty");
        }
        //检测该邮箱是否被注册
        else if (userService.UserExist(UserEmail.trim())) {
            jsonObject.put("UserRegisterResult", "Existed");
        } else {
            User user = new User();

            user.setUserName(UserName.trim());
            if (UserAddress == null) {
                UserAddress = "Undifined";
            }
            user.setUserAddress(UserAddress.trim());
            user.setUserEmail(UserEmail.trim());
            user.setAdmin(false);
            user.setPassword(Password.trim());
            user.setCreateDate(new Date(System.currentTimeMillis()));

            if (userService.insertUser(user)) {
                jsonObject.put("UserRegisterResult", "Success");
                jsonObject.put("UserRegisterObjectResult",user );
            }



        }
        return jsonObject.toJSONString();
    }


    //用户登录
    @PostMapping("/Login")
    public String UserLogin(@RequestParam(value = "UserEmail", required = false) String UserEmail,
                            @RequestParam(value = "Password", required = false) String Password) {


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("UserLoginResult", "Fail");

        /*后端校验数据合法性*/
        if (UserEmail == null || Password == null) {
            jsonObject.put("UserLoginResult", "Parameter-Null");

        } else if (UserEmail.trim().isEmpty() || Password.trim().isEmpty()) {
            jsonObject.put("UserLoginResult", "Empty");

            /*用户邮箱与密码是否匹配*/
        } else if (!userService.UserExist(UserEmail.trim())) {
            jsonObject.put("UserDeleteResult", "Wrong-Email");
        } else {
            User user = userService.findUserByEmail(UserEmail.trim());
            if (user != null && Password.equals(user.getPassword())) {
                jsonObject.put("UserLoginResult", "Success");
                jsonObject.put("UserLoginObjectResult", user);
            }
        }

        return jsonObject.toJSONString();

    }



    /**
     * 查询个人信息
     *
     * @param UserEmail:邮箱
     */
    @PostMapping("/Get")
    public String UserGet(@RequestParam(value = "UserEmail", required = false) String UserEmail) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("UserGetResult", "Fail");

        /*后端校验数据合法性*/
        if (UserEmail == null) {
            jsonObject.put("UserGetResult", "Parameter-Null");

        } else if (UserEmail.trim().isEmpty()) {
            jsonObject.put("UserGetResult", "Empty");
        } else if (!userService.UserExist(UserEmail.trim())) {
            jsonObject.put("UserGetResult", "Wrong-Email");
        } else {
            User user = userService.findUserByEmail(UserEmail.trim());
            if (user != null) {
                jsonObject.put("UserGetResult", "Success");
                jsonObject.put("UserGetObjectResult", user);
            }
        }

        return jsonObject.toJSONString();
    }


    /**
     * 修改个人信息（地址、名字）
     *
     * @param UserEmail:邮箱判断是哪个用户，不允许更新
     * @param UserAddress:用于更改
     * @param UserName:用于更改
     */
    @PostMapping("/Update")
    public String UserUpdate(@RequestParam(value = "UserEmail", required = false) String UserEmail,
                             @RequestParam(value = "UserName", required = false) String UserName,
                             @RequestParam(value = "UserAddress", required = false) String UserAddress) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("UserUpdateResult", "Fail");

        /*后端校验数据合法性*/
        if (UserEmail == null
                || UserName == null
                || UserAddress == null) {
            jsonObject.put("UserUpdateResult", "Parameter-Null");

        } else if (UserEmail.trim().isEmpty()
                || UserName.trim().isEmpty()
                || UserAddress.trim().isEmpty()) {
            jsonObject.put("UserUpdateResult", "Empty");
        } else if (!userService.UserExist(UserEmail.trim())) {
            jsonObject.put("UserUpdateResult", "Wrong-Email");
        } else {
            User user = userService.findUserByEmail(UserEmail.trim());
            if (user != null) {
                userService.updateUser(UserEmail.trim(), UserName.trim(), UserAddress.trim());
                jsonObject.put("UserUpdateResult", "Success！");
            }
        }

        return jsonObject.toJSONString();
    }

    /**
     * 修改密码
     *
     * @param UserEmail:邮箱判断是哪个用户，不允许更新
     * @param NewPassword:用于更改
     */
    @PostMapping("/ResetPassword")
    public String UserResetPassword(@RequestParam(value = "UserEmail", required = false) String UserEmail,
                                    @RequestParam(value = "NewPassword", required = false) String NewPassword) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("UserResetPasswordResult", "Fail");

        /*后端校验数据合法性*/
        if (UserEmail == null
                || NewPassword == null) {
            jsonObject.put("UserResetPasswordResult", "Parameter-Null");

        } else if (UserEmail.trim().isEmpty()
                || NewPassword.trim().isEmpty()) {
            jsonObject.put("UserResetPasswordResult", "Empty");
        }

        //判断是否被存在该用户
        else if (!userService.UserExist(UserEmail.trim())) {
            jsonObject.put("UserResetPasswordResult", "Wrong-Email");
        } else {

            User user = userService.findUserByEmail(UserEmail.trim());

            if (user != null) {

                if (NewPassword.equals(user.getPassword())) {
                    jsonObject.put("UserResetPasswordResult", "Identical");
                } else {
//                    user.setPassword(NewPassword.trim());
//                    user.setUserEmail(UserEmail.trim());

                    if (userService.updatePassword(UserEmail.trim(), NewPassword.trim())) {
                        jsonObject.put("UserResetPasswordResult", "Success");
                    }
                }

            }


        }
        return jsonObject.toJSONString();

    }

    //删除用户
    @PostMapping("/DeleteUser")
    public String UserDelete(@RequestParam(value = "UserEmail", required = false) String UserEmail) {
        /*初始化返回结果，并将结果RegisterResult字段置为Fail*/
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("UserDeleteResult", "Fail");

        /*后端校验数据合法性*/
        if (UserEmail == null) {
            jsonObject.put("UserDeleteResult", "Parameter-Null");

        } else if (UserEmail.trim().isEmpty()) {
            jsonObject.put("UserDeleteResult", "Empty");
        } else if (!userService.UserExist(UserEmail.trim())) {
            jsonObject.put("UserDeleteResult", "Wrong-Email");
        } else if (userService.userDelete(UserEmail.trim())) {
            jsonObject.put("UserDeleteResult", "Success");
        }
        return jsonObject.toJSONString();
    }

    /**
     *
     * @param Index
     * @param Size
     * @return
     */
    @PostMapping("/GetList")
    public String UserGetList(@RequestParam(value = "Index", required = false) String Index,
                              @RequestParam(value = "Size", required = false) String Size) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("UserGetListResult", "Fail");

        /*后端校验数据合法性*/
        if (Index == null
                || Size == null) {

            jsonObject.put("UserGetListResult", "Parameter-Null");

        } else if (Index.trim().isEmpty()
                || Size.trim().isEmpty()) {
            jsonObject.put("UserGetListResult", "Empty");
        } else {

            jsonObject.put("UserGetListResult", "Success");
            jsonObject.put("UserGetListObjectResult", userService.pagedFindById(Index, Size));

        }

        return jsonObject.toJSONString();

    }

    @PostMapping("/GetListSize")
    public String UserGetListSize() {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("UserGetListSizeResult", userService.getLength());

        return jsonObject.toJSONString();

    }

    @PostMapping("/Find")
    public String UserFind(@RequestParam(value = "LikeName", required = false) String LikeName) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("UserFindResult", "Fail");

        if (LikeName == null) {
            jsonObject.put("UserFindResult", "Parameter-Null");
        } else if (LikeName.trim().isEmpty()) {
            jsonObject.put("UserFindResult", "Empty");
        } else {
            List<User> userList = userService.findByLikeName(LikeName);
            if (userList.size() != 0) {
                jsonObject.put("UserFindResult", "Success");
                jsonObject.put("UserFindObjectResult", userList);
            }
        }

        return jsonObject.toJSONString();
    }

    @PostMapping("/FindAll")
    public String UserFindAll(){
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("UserFindAllResult", "Fail");

        jsonObject.put("UserFindAllResult", userService.showAllUser());

        return jsonObject.toJSONString();
    }


}
